package com.bw.goods.controller;

import java.nio.file.Watchable;
import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.goods.config.JwtUtil;
import com.bw.goods.config.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bw.goods.entity.UserEntity;
import com.bw.goods.service.UserService;
import com.bw.common.utils.PageUtils;
import com.bw.common.utils.R;



/**
 * 这是个用户表
 *
 * @author jwcz
 * @email 2898807664@qq.com
 * @date 2022-02-22 15:35:36
 */
@RestController
@RequestMapping("goods/user")
public class UserController {
    @Autowired
    private UserService userService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }
    /**
     * 列表
     */
    @RequestMapping("/login")
    public R login(@RequestBody UserEntity userEntity){
        return userService.login(userEntity);
    }
    /**
     * 信息
     */
    @RequestMapping("/register")
    public R register(@RequestBody UserEntity userEntity){
        QueryWrapper<UserEntity> nickname = new QueryWrapper<UserEntity>().eq("nickname", userEntity.getNickname());
        UserEntity entity = userService.getOne(nickname);
        if(entity!=null) {
            return R.error("该用户已注册");
        }else {
            userService.register(userEntity);
            return R.ok();
        }
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		UserEntity user = userService.getById(id);

        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody UserEntity user){
		userService.save(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody UserEntity user){
		userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		userService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
